package com.tanhua.server.controller;

import com.tanhua.commons.utils.UserHolder;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.domain.mysql.UserInfo;
import com.tanhua.server.service.LoginService;
import com.tanhua.server.service.UserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

/**
 * @author lxb哦吼吼
 */
@RestController
@RequestMapping({"/user", "/huanxin"})
public class LoginController {

    @Autowired
    private LoginService loginService;

    @Autowired
    private UserService userService;

    @DubboReference
    private UserInfoApi userInfoApi;

    /**
     * description: 接收手机号,发送验证码,请求登录 <br>
     * version: 1.0 <br>
     * date: 2021/11/14 18:01 <br>
     * author: SangWu <br>
     *
     * @param map
     * @return org.springframework.http.ResponseEntity
     */
    @PostMapping("/login")
    public ResponseEntity login(@RequestBody Map<String, String> map) {
        String phone = map.get("phone");
        loginService.sendMsg(phone);
        return ResponseEntity.ok(null);
    }

    /**
     * description: 登录验证,对比验证码 <br>
     * version: 1.0 <br>
     * date: 2021/11/14 18:05 <br>
     * author: SangWu <br>
     *
     * @param map
     * @return org.springframework.http.ResponseEntity
     */
    @PostMapping("/loginVerification")
    public ResponseEntity loginVerification(@RequestBody Map<String, String> map) {
        //1、调用map集合获取请求参数
        String phone = map.get("phone");
        String code = map.get("verificationCode");
        //2、调用userService完成用户登录
        Map restMap = loginService.loginVerification(phone, code);
        //3、构造返回
        return ResponseEntity.ok(restMap);
    }

    /**
     * description: 新用户信息保存 <br>
     * version: 1.0 <br>
     * date: 2021/11/14 19:15 <br>
     * author: SangWu <br>
     *
     * @param userInfo
     * @return org.springframework.http.ResponseEntity
     */
    @PostMapping("/loginReginfo")
    public ResponseEntity firstSaveUserInfo(@RequestBody UserInfo userInfo) {
        userInfo.setId(UserHolder.getUserId());
        userInfoApi.save(userInfo);
        return ResponseEntity.ok(null);
    }

    /**
     * description: 新用户上传头像 <br>
     * version: 1.0 <br>
     * date: 2021/11/14 20:20 <br>
     * author: SangWu <br>
     *
     * @param headPhoto
     * @return org.springframework.http.ResponseEntity
     */
    @PostMapping("/loginReginfo/head")
    public ResponseEntity firstSaveHead(MultipartFile headPhoto) throws IOException {
        userService.uploadHead(headPhoto);
        return ResponseEntity.ok(null);
    }

    /**
     * 获取环信账号和密码
     * @return
     */
    @GetMapping("/user")
    public ResponseEntity findHuanXin() {
        Map<String, String> map = loginService.findHuanXin();
        return ResponseEntity.ok(map);
    }
}
